#include <nterm.h>
#include <stdarg.h>
#include <unistd.h>
#include <SDL.h>

char handle_key(SDL_Event *ev);

static void sh_printf(const char *format, ...) {
  static char buf[256] = {};
  va_list ap;
  va_start(ap, format);
  int len = vsnprintf(buf, 256, format, ap);
  va_end(ap);
  term->write(buf, len);
}

static void sh_banner() {
  sh_printf("Built-in Shell in NTerm (NJU Terminal)\n\n");
}

static void sh_prompt() {
  sh_printf("sh> ");
}

static void sh_handle_cmd(const char *cmd) {
  int i = 0;
  char *name;
  char *PATH;
  char arg[64];
  strcpy(arg,cmd);
  // printf("i=%d,char = %s\n",i,cmd);
  name = strtok(arg, "=");
  PATH = strtok(NULL, "\n");
  // printf("name=%s\n",name);
  // printf("PATH=%s\n",PATH);
  if (PATH!=NULL)
  {
      
      setenv(name,PATH,0);
  }
  
  else if (execvp(cmd,NULL)==-1)
  {
    printf("error cmd\n");
  }
  
}

void builtin_sh_run() {
  sh_banner();
  sh_prompt();

  while (1) {
    SDL_Event ev;
    if (SDL_PollEvent(&ev)) {
      if (ev.type == SDL_KEYUP || ev.type == SDL_KEYDOWN) {
        const char *res = term->keypress(handle_key(&ev));
        if (res) {
          sh_handle_cmd(res);
          sh_prompt();
        }
      }
    }
    refresh_terminal();
  }
}
